home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / printing / std file saver / source / mypdef_4_handlingdialogs.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  23.8 KB  |  876 lines

  1. /*
  2. ** Copyright 1991-1996 Apple Computer. All rights reserved.
  3. **
  4. **    You may incorporate this sample code into your applications without
  5. **    restriction, though the sample code has been provided "AS IS" and the
  6. **    responsibility for its operation is 100% yours.  However, what you are
  7. **    not permitted to do is to redistribute the source as "DSC Sample Code"
  8. **    after having made changes. If you're going to re-distribute the source,
  9. **    we require that you make it clear in the source that the code was
  10. **    descended from Apple Sample Code, but that you've made changes.
  11. */
  12.  
  13. #include <QuickDraw.h>
  14. #include <Dialogs.h>
  15. #include <Printing.h>
  16. #include <Resources.h>
  17. #include <Errors.h>
  18. #include <Memory.h>
  19. #include <TextUtils.h>
  20. #include <OSUtils.h>
  21.  
  22. #include "DriverTypes.h"
  23.  
  24. pascal void HandleStyleItems(TPPrDlg theDlg, short ItemHit);
  25. pascal void HandleJobItems(TPPrDlg theDlg, short ItemHit);
  26. pascal void FilePrintDefault(THPrint hPrint);
  27. pascal Boolean FilePrStlDialog(THPrint hPrint);
  28. pascal Boolean FilePrJobDialog(THPrint hPrint);
  29. pascal TPPrDlg FilePrStlInit(THPrint hPrint);
  30. pascal TPPrDlg FilePrJobInit(THPrint hPrint);
  31. pascal Boolean FilePrDlgMain(THPrint hPrint, PDlgInitProcPtr initFunc);
  32. pascal Boolean FilePrValidate(THPrint hPrint);
  33. pascal void FilePrJobMerge(THPrint hPrintSrc, THPrint hPrintDst);
  34.  
  35. #if defined(__MWERKS__)
  36. asm void __Startup__ (void);
  37. asm void __Startup__ (void)
  38. {
  39.     JMP    FilePrintDefault
  40.     JMP    FilePrStlDialog
  41.     JMP    FilePrJobDialog
  42.     JMP    FilePrStlInit
  43.     JMP    FilePrJobInit
  44.     JMP    FilePrDlgMain
  45.     JMP    FilePrValidate
  46.     JMP    FilePrJobMerge
  47. }
  48. #endif
  49.  
  50. void SetDialogControlValue(DialogPtr theDialog, short theItem, short theValue)
  51. {
  52.     short theType;
  53.     Handle theHandle;
  54.     Rect theRect;
  55.  
  56.     GetDialogItem(theDialog, theItem, &theType, &theHandle, &theRect);
  57.     if (!theHandle) return;
  58.     SetCtlValue((ControlHandle)theHandle, theValue);
  59. }
  60.  
  61. short GetDialogControlValue(DialogPtr theDialog, short theItem)
  62. {
  63.     short theType;
  64.     Handle theHandle;
  65.     Rect theRect;
  66.  
  67.     GetDialogItem(theDialog, theItem, &theType, &theHandle, &theRect);
  68.     if (!theHandle) return -1;
  69.     return GetCtlValue((ControlHandle)theHandle);
  70. }
  71.  
  72. void SetDialogItemString(DialogPtr theDialog, short theItem, StringPtr theString)
  73. {
  74.     short theType;
  75.     Handle theHandle;
  76.     Rect theRect;
  77.  
  78.     GetDialogItem(theDialog, theItem, &theType, &theHandle, &theRect);
  79.     if (!theHandle) return;
  80.     SetIText(theHandle, theString);
  81. }
  82.  
  83. void GetDialogItemString(DialogPtr theDialog, short theItem, StringPtr theString)
  84. {
  85.     short theType;
  86.     Handle theHandle;
  87.     Rect theRect;
  88.  
  89.     GetDialogItem(theDialog, theItem, &theType, &theHandle, &theRect);
  90.     if (!theHandle) return;
  91.     GetIText(theHandle, theString);
  92. }
  93.  
  94. void SetDialogItemNumber(DialogPtr theDialog, short theItem, long theValue)
  95. {
  96.     short theType;
  97.     Handle theHandle;
  98.     Rect theRect;
  99.     Str255 theStr;
  100.  
  101.     GetDialogItem(theDialog, theItem, &theType, &theHandle, &theRect);
  102.     if (!theHandle) return;
  103.     NumToString(theValue, theStr);
  104.     SetIText(theHandle, theStr);
  105. }
  106.  
  107. long GetDialogItemNumber(DialogPtr theDialog, short theItem)
  108. {
  109.     Str255 theStr;
  110.     long theValue;
  111.  
  112.     GetDialogItemString(theDialog, theItem, theStr);
  113.     StringToNum(theStr, &theValue);
  114.     return theValue;
  115. }
  116.  
  117. Rect GetDialogItemRect(DialogPtr theDialog, short theItem)
  118. {
  119.     short theType;
  120.     Handle theHandle;
  121.     Rect theRect;
  122.  
  123.     GetDialogItem(theDialog, theItem, &theType, &theHandle, &theRect);
  124.     return theRect;
  125. }
  126.  
  127. void SelectDialogItemString(DialogPtr theDialog, short theItem)
  128. {
  129.     SelIText(theDialog, theItem, startSelectAll, endSelectAll);
  130. }
  131.  
  132. void InvertDialogItem(DialogPtr theDialog, short theItem)
  133. {
  134.     Rect    theRect;
  135.  
  136.     theRect = GetDialogItemRect(theDialog, theItem);
  137.     InvertRect(&theRect);
  138. }
  139.  
  140. void DialogPosition(DialogPtr theDialog)
  141. {
  142. #pragma unused(theDialog)
  143.     /* this is a no-op here, since we're specifying the swell
  144.     ** system 7 dialog positioning in the DLOG resources. If
  145.     ** you want to support System 6, or have a position other
  146.     ** than "Alert position, main screen", you'll need to flesh
  147.     ** out this function.
  148.     */
  149. }
  150.  
  151. Handle ReallyGetResource(ResType theType, short resID)
  152. {
  153.     Handle    theRes = GetResource(theType,resID);
  154.     if(!theRes) return 0;
  155.     LoadResource(theRes);
  156.     return theRes;
  157. }
  158.  
  159. void FrameDialogItem(DialogPtr theDialog, short theItem)
  160. {
  161.     GrafPtr    oldPort;
  162.     Rect    itsBox = GetDialogItemRect((DialogPtr)theDialog, theItem);
  163.  
  164.     GetPort(&oldPort);
  165.     SetPort((GrafPtr)theDialog);
  166.     PenSize(3, 3);
  167.     InsetRect(&itsBox, -4, -4);
  168.     {
  169.         short    radius = (itsBox.top - itsBox.bottom) / 2;
  170.         if (radius < 16) radius = 16;
  171.         FrameRoundRect(&itsBox, radius, radius);
  172.     }
  173.     SetPort(oldPort);
  174. }
  175.  
  176. pascal void FilePrintDefault(THPrint hPrint)
  177. {
  178.     // the default in is the resource PREC 0, and we also initialize some important fields 
  179.     THPrint theDefault;
  180.  
  181.     theDefault = (THPrint)ReallyGetResource('PREC', 0);
  182.     if (theDefault == NULL) {
  183.         setPrintErr(resNotFound);
  184.         return;
  185.     }
  186.     **hPrint = **theDefault;
  187.  
  188.     if (StripAddress((Ptr)*hPrint) != StripAddress((Ptr)*theDefault)) 
  189.         ReleaseResource((Handle)theDefault);
  190. }
  191.  
  192. pascal Boolean FilterNumPrStlAndJob(DialogPtr theDialog, EventRecord *theEvent, short *ItemHit)
  193. {
  194. #pragma unused(theDialog)
  195.     if ((theEvent->what == keyDown) || (theEvent->what == autoKey)) {
  196.         char    theChar;
  197.         theChar = theEvent->message & charCodeMask;
  198.         if ((theChar == enterKey) || (theChar == returnKey)) {
  199.             *ItemHit = printOkButton;
  200.             return true;
  201.         } else if ((theChar!=backspaceKey)&&(theChar!=tabKey)&&(!isdigit(theChar))) {
  202.             SysBeep(1);
  203.             *ItemHit = printInvalidItem;
  204.             return true;
  205.         }
  206.     }
  207.     return false;
  208. }
  209.  
  210. void StlHideCustomSizeItems(DialogPtr theDialog)
  211. {
  212.     short item;
  213.  
  214.     if (!theDialog) return;
  215.  
  216.     for(item = stlHorizTitleID; item <= stlPixelsButtonID; item++) 
  217.         HideDItem(theDialog, item);
  218. }
  219.  
  220. void StlShowCustomSizeItems(DialogPtr theDialog)
  221. {
  222.     short item;
  223.     
  224.     if (!theDialog) return;
  225.  
  226.     for (item = stlHorizTitleID; item <= stlPixelsButtonID; item++) 
  227.         ShowDItem(theDialog, item);
  228. }
  229.  
  230. pascal Boolean FilePrValidate(THPrint hPrint)
  231. {
  232.     // if the signature mySignature is present in the hPrint, then I
  233.     // consider that the values are ok else we just load anew the
  234.     // resource PREC 0
  235.     // this would be a bit more robust in a real driver
  236.  
  237.     THPrint theDefault;
  238.     Boolean changedIt = false;
  239.  
  240.     theDefault = (THPrint)ReallyGetResource('PREC', 0);
  241.     if (theDefault == NULL) {
  242.         DebugStr("\pCouldn't find the PREC 0 resource. That doesn't seem good.");
  243.         setPrintErr(resNotFound);
  244.         return false;
  245.     }
  246.     
  247.     if ((**hPrint).printX[7] != mySignature) {
  248.         **hPrint = **theDefault;
  249.         changedIt = true;
  250.     }
  251.  
  252.     ReleaseResource((Handle)theDefault);
  253.     
  254.     (**hPrint).prJob.iCopies = 1;
  255.     (**hPrint).prJob.pIdleProc = NULL;
  256.     (**hPrint).prJob.pFileName = NULL;
  257.     (**hPrint).prJob.iFileVol = 0;
  258.     (**hPrint).prJob.bFileVers = 0;
  259.     
  260.     return changedIt;
  261. }
  262.  
  263. pascal TPPrDlg FilePrStlInit(THPrint hPrint)
  264. {
  265.     // we initialize the dialog items with the right settings coming from hPrint 
  266.     TPPrDlg theDlg;
  267.     DialogPtr aDlg;
  268.     THPrint theDefault;
  269.  
  270.     theDefault = (THPrint)ReallyGetResource('PREC', 1);
  271.     if (theDefault != NULL) 
  272.         **hPrint = **theDefault;
  273.  
  274.     theDlg = (TPPrDlg)NewPtr(sizeof(TPrDlg));
  275.     if (theDlg == NULL) {
  276.         setPrintErr(iMemFullErr);
  277.         return 0;
  278.     }
  279.     
  280.     theDlg->pFltrProc = NewModalFilterProc(FilterNumPrStlAndJob);
  281.     theDlg->pItemProc = NewPItemProc(HandleStyleItems);
  282.     theDlg->fDoIt = false;
  283.     theDlg->fDone = false;
  284.     theDlg->hPrintUsr = hPrint;
  285.  
  286.     aDlg = GetNewDialog(printDialogID, theDlg, (WindowPtr)-1L);
  287.     if (aDlg == NULL) {
  288.         setPrintErr(resNotFound);
  289.         DisposePtr((Ptr)theDlg);
  290.         return 0;
  291.     }
  292.     DialogPosition(aDlg);
  293.  
  294.     switch((**hPrint).printX[0]) {
  295.         default:
  296.             DebugStr("\pBad value for paper size");
  297.             (**hPrint).printX[0] = printOnA4;
  298.             // fall through
  299.         case printOnA4:
  300.             SetDialogControlValue(aDlg, stlPaperA4SizeButtonID, 1);
  301.             SetDialogControlValue(aDlg, stlPaperLetterSizeButtonID, 0);
  302.             SetDialogControlValue(aDlg, stlPaperCustomSizeButtonID, 0);
  303.             StlHideCustomSizeItems(aDlg);
  304.             break;
  305.         case printOnLetter:
  306.             SetDialogControlValue(aDlg, stlPaperA4SizeButtonID, 0);
  307.             SetDialogControlValue(aDlg, stlPaperLetterSizeButtonID, 1);
  308.             SetDialogControlValue(aDlg, stlPaperCustomSizeButtonID, 0);
  309.             StlHideCustomSizeItems(aDlg);
  310.             break;
  311.         case printOnCustom:
  312.             SetDialogControlValue(aDlg, stlPaperA4SizeButtonID, 0);
  313.             SetDialogControlValue(aDlg, stlPaperLetterSizeButtonID, 0);
  314.             SetDialogControlValue(aDlg, stlPaperCustomSizeButtonID, 1);
  315.             StlShowCustomSizeItems(aDlg);
  316.             break;
  317.     }
  318.  
  319.     SetDialogItemNumber(aDlg, stlHorizTextID, (**hPrint).printX[2]);
  320.     SetDialogItemNumber(aDlg, stlVertTextID, (**hPrint).printX[3]);
  321.     switch((**hPrint).printX[4]) {
  322.         default:
  323.             DebugStr("\pBad value for units...repairing...");
  324.             (**hPrint).printX[4] = unitsMilliInch;
  325.             // fall through
  326.         case unitsMilliInch:
  327.             SetDialogControlValue(aDlg, stlMilliInchButtonID, 1);
  328.             SetDialogControlValue(aDlg, stlMilliMeterButtonID, 0);
  329.             SetDialogControlValue(aDlg, stlPixelsButtonID, 0);
  330.             break;
  331.         case unitsMilliMeter:
  332.             SetDialogControlValue(aDlg, stlMilliInchButtonID, 0);
  333.             SetDialogControlValue(aDlg, stlMilliMeterButtonID, 1);
  334.             SetDialogControlValue(aDlg, stlPixelsButtonID, 0);
  335.             break;
  336.         case unitsPixels:
  337.             SetDialogControlValue(aDlg, stlMilliInchButtonID, 0);
  338.             SetDialogControlValue(aDlg, stlMilliMeterButtonID, 0);
  339.             SetDialogControlValue(aDlg, stlPixelsButtonID, 1);
  340.             break;
  341.     }
  342.     ShowWindow(aDlg);
  343.     DrawDialog(aDlg);
  344.     SetPort(aDlg);
  345.     theDlg->lUser1 = (**hPrint).printX[5];
  346.     if ((**hPrint).printX[5])
  347.         InvertDialogItem(aDlg,stlLandscapeIconID);
  348.     else
  349.         InvertDialogItem(aDlg,stlPortraitIconID);
  350.     ValidRect(&(aDlg->portRect));
  351.     return theDlg;
  352. }
  353.  
  354. // redraw the frame around the ok button, and pay attention to the orientation icons 
  355. void RefreshDialog(TPPrDlg thePrDlg)
  356. {
  357.  
  358.     DrawDialog((DialogPtr)thePrDlg);
  359.     FrameDialogItem((DialogPtr)thePrDlg,printOkButton);
  360.  
  361.     if (thePrDlg->lUser1)
  362.         InvertDialogItem((DialogPtr)thePrDlg,stlLandscapeIconID);
  363.     else
  364.         InvertDialogItem((DialogPtr)thePrDlg,stlPortraitIconID);
  365.     ValidRect(&(((GrafPtr)thePrDlg)->portRect));
  366. }
  367.  
  368. void HandleStlHelp(void) {
  369.     short        item;
  370.     DialogPtr    helpDlg = GetNewDialog(helpDialogID, NULL, (WindowPtr)-1);
  371.  
  372.     if (!helpDlg) {
  373.         SysBeep(1);
  374.         return;
  375.     }
  376.  
  377.     for(item = 6; item <= 11; item++) 
  378.         HideDItem(helpDlg, item);
  379.  
  380.     ShowWindow(helpDlg);
  381.     DrawDialog(helpDlg);
  382.     SetPort((GrafPtr)helpDlg);
  383.     ValidRect(&(helpDlg->portRect));
  384.  
  385.     TextFont(1);
  386.     TextSize(9);
  387.  
  388.     for(item = 6; item <= 11; item++) 
  389.         ShowDItem(helpDlg, item);
  390.  
  391.     FrameDialogItem(helpDlg,1);
  392.     
  393.     while(item!=printOkButton)
  394.         ModalDialog(NULL, &item);
  395.  
  396.     DisposeDialog(helpDlg);
  397. }
  398.  
  399. // the paper sizes in the following routines come from the LaserWriter ppd
  400. // I've used the full-paper size as the size for the PICT. You'll probably
  401. // want to use something like the imageable area, but by making them this
  402. // size, it'll get you to look here to fix up the sizes. All the sizes
  403. // could be stored in resources (use the 'pgsz' maybe) as well, but this is
  404. // a simple sample.
  405. static void SetPaperA4(long *height, long *width, Boolean isLandscape)
  406. {
  407.     const short a4PaperWidth = 595;
  408.     const short a4PaperHeight = 842;
  409.  
  410.     if (!isLandscape) {
  411.         *width = a4PaperWidth;
  412.         *height = a4PaperHeight;
  413.     } else {
  414.         *width = a4PaperHeight;
  415.         *height = a4PaperWidth;
  416.     }
  417. }
  418.  
  419. static void SetPaperLetter(long *height, long *width, Boolean isLandscape)
  420. {
  421.     const short letterPaperWidth = 612;
  422.     const short letterPaperHeight = 792;
  423.  
  424.     if (!isLandscape) {
  425.         *width = letterPaperWidth;
  426.         *height = letterPaperHeight;
  427.     } else {
  428.         *width = letterPaperHeight;
  429.         *height = letterPaperWidth;
  430.     }
  431. }
  432.  
  433. static void SetPaperLegal(long *height, long *width, Boolean isLandscape)
  434. {
  435.     const short legalPaperWidth = 612;
  436.     const short legalPaperHeight = 1008;
  437.  
  438.     if (!isLandscape) {
  439.         *width = legalPaperWidth;
  440.         *height = legalPaperHeight;
  441.     } else {
  442.         *width = legalPaperHeight;
  443.         *height = legalPaperWidth;
  444.     }
  445. }
  446.  
  447. static void SetPaperCustom(long *height, long *width, Boolean isLandscape,
  448.     short units, long rawHeight, long rawWidth)
  449. {
  450.     float    scaleFactor;
  451.     long    scaleWidth,scaleHeight;
  452.  
  453.     switch (units) {
  454.         case unitsMilliInch:    scaleFactor = 0.072;        break;
  455.         case unitsMilliMeter:    scaleFactor = 720.0/254;    break;
  456.         case unitsPixels:        scaleFactor = 1.0;            break;
  457.         default:                scaleFactor = 1.0;            break;    // have to set it to SOMETHING
  458.     }
  459.     
  460.     scaleWidth = rawWidth * scaleFactor;
  461.     scaleHeight = rawHeight * scaleFactor;
  462.  
  463.     if (!isLandscape) {
  464.         *width = rawWidth;
  465.         *height = rawHeight;
  466.     } else {
  467.         *width = rawHeight;
  468.         *height = rawWidth;
  469.     }
  470. }
  471.  
  472. static Boolean SetPaperSize(THPrint hPrintUsr)
  473. {
  474.     long    height=0,width=0;
  475.  
  476.     switch((**hPrintUsr).printX[0]) {
  477.         case printOnA4:
  478.             SetPaperA4(&height,&width,(**hPrintUsr).printX[5]);
  479.             break;
  480.         case printOnLetter:
  481.             SetPaperLetter(&height,&width,(**hPrintUsr).printX[5]);
  482.             break;
  483.         case printOnCustom:
  484.             SetPaperCustom(&height,&width,(**hPrintUsr).printX[5],
  485.                 (**hPrintUsr).printX[4],(**hPrintUsr).printX[3],(**hPrintUsr).printX[2]);
  486.             break;
  487.         case printOnLegal:
  488.             SetPaperLegal(&height,&width,(**hPrintUsr).printX[5]);
  489.             break;
  490.         default:
  491.             return false;
  492.             break;
  493.     }
  494.     
  495.     SetRect(&((**hPrintUsr).rPaper), 0, 0, width, height);
  496.     (**hPrintUsr).prInfo.rPage = (**hPrintUsr).rPaper;
  497.     (**hPrintUsr).prInfoPT.rPage = (**hPrintUsr).rPaper;
  498.     (**hPrintUsr).prStl.iPageV = (height * iPrPgFract) / 72;
  499.     (**hPrintUsr).prStl.iPageH = (width * iPrPgFract) / 72;
  500.     return true;
  501. }
  502.  
  503. pascal void HandleStyleItems(TPPrDlg thePrDlg, short ItemHit)
  504. {
  505.     THPrint hPrintUsr = thePrDlg->hPrintUsr;
  506.     DialogPtr    theDialog = (DialogPtr)(thePrDlg);
  507.  
  508.     switch (ItemHit) { 
  509.         case printOkButton:
  510.             if (GetDialogControlValue(theDialog, stlPaperA4SizeButtonID) == 1) {
  511.                 (**hPrintUsr).printX[0] = printOnA4;
  512.                 (**hPrintUsr).printX[2] = 0;
  513.                 (**hPrintUsr).printX[3] = 0;
  514.                 (**hPrintUsr).printX[4] = unitsUndefined;
  515.             } else if (GetDialogControlValue(theDialog, stlPaperLetterSizeButtonID) == 1) {
  516.                 (**hPrintUsr).printX[0] = printOnLetter;
  517.                 (**hPrintUsr).printX[2] = 0;
  518.                 (**hPrintUsr).printX[3] = 0;
  519.                 (**hPrintUsr).printX[4] = unitsUndefined;
  520.             } else if (GetDialogControlValue(theDialog, stlPaperCustomSizeButtonID) == 1) {
  521.                 (**hPrintUsr).printX[0] = printOnCustom;
  522.                 if (GetDialogControlValue(theDialog, stlMilliInchButtonID) == 1)
  523.                     (**hPrintUsr).printX[4] = unitsMilliInch;
  524.                 if (GetDialogControlValue(theDialog, stlMilliMeterButtonID) == 1)
  525.                     (**hPrintUsr).printX[4] = unitsMilliMeter;
  526.                 if (GetDialogControlValue(theDialog, stlPixelsButtonID) == 1)
  527.                     (**hPrintUsr).printX[4] = unitsPixels;
  528.     
  529.                 (**hPrintUsr).printX[2] = GetDialogItemNumber(theDialog, stlHorizTextID);
  530.                 (**hPrintUsr).printX[3] = GetDialogItemNumber(theDialog, stlVertTextID);
  531.             } else {
  532.                 (**hPrintUsr).printX[0] = printOnUndefined;
  533.                 (**hPrintUsr).printX[2] = 0;
  534.                 (**hPrintUsr).printX[3] = 0;
  535.                 (**hPrintUsr).printX[4] = unitsUndefined;
  536.             }
  537.  
  538.             (**hPrintUsr).printX[5] = (thePrDlg->lUser1 != 0);
  539.  
  540.             if (SetPaperSize(hPrintUsr)) {
  541.                 thePrDlg->fDone = true;
  542.                 thePrDlg->fDoIt = true;
  543.             } else {
  544.                 NoteAlert(badValueAlertID,nil);
  545.             }
  546.             break;
  547.         case printCancelButton:
  548.             thePrDlg->fDone = true;
  549.             thePrDlg->fDoIt = false;
  550.             break;
  551.         case stlPaperA4SizeButtonID:
  552.             if (GetDialogControlValue(theDialog, ItemHit) == 0) {
  553.                 if (GetDialogControlValue(theDialog, stlPaperCustomSizeButtonID)) 
  554.                     StlHideCustomSizeItems(theDialog);
  555.                 SetDialogControlValue(theDialog, stlPaperCustomSizeButtonID, 0);
  556.                 SetDialogControlValue(theDialog, stlPaperLetterSizeButtonID, 0);
  557.                 SetDialogControlValue(theDialog, stlPaperA4SizeButtonID, 1);
  558.             }
  559.             break;
  560.         case stlPaperLetterSizeButtonID:
  561.             if (GetDialogControlValue(theDialog, ItemHit) == 0) {
  562.                 if (GetDialogControlValue(theDialog, stlPaperCustomSizeButtonID)) 
  563.                     StlHideCustomSizeItems(theDialog);
  564.                 SetDialogControlValue(theDialog, stlPaperCustomSizeButtonID, 0);
  565.                 SetDialogControlValue(theDialog, stlPaperLetterSizeButtonID, 1);
  566.                 SetDialogControlValue(theDialog, stlPaperA4SizeButtonID, 0);
  567.             }
  568.             break;
  569.         case stlPaperCustomSizeButtonID:
  570.             if (GetDialogControlValue(theDialog, ItemHit) == 0) {
  571.                 StlShowCustomSizeItems(theDialog);
  572.                 SetDialogControlValue(theDialog, stlPaperCustomSizeButtonID, 1);
  573.                 SetDialogControlValue(theDialog, stlPaperLetterSizeButtonID, 0);
  574.                 SetDialogControlValue(theDialog, stlPaperA4SizeButtonID, 0);
  575.             }
  576.             break;
  577.         case stlPortraitIconID:
  578.             if (thePrDlg->lUser1) {
  579.                 InvertDialogItem(theDialog, stlPortraitIconID);
  580.                 InvertDialogItem(theDialog, stlLandscapeIconID);
  581.                 thePrDlg->lUser1 = false;
  582.             }
  583.             break;
  584.         case stlLandscapeIconID:
  585.             if (!thePrDlg->lUser1) {
  586.                 InvertDialogItem(theDialog, stlPortraitIconID);
  587.                 InvertDialogItem(theDialog, stlLandscapeIconID);
  588.                 thePrDlg->lUser1 = true;
  589.             }
  590.         case stlMilliInchButtonID:
  591.         case stlMilliMeterButtonID: 
  592.         case stlPixelsButtonID: 
  593.             if (GetDialogControlValue(theDialog, ItemHit) == 0) {
  594.                 SetDialogControlValue(theDialog, stlMilliInchButtonID, 0);
  595.                 SetDialogControlValue(theDialog, stlMilliMeterButtonID, 0);
  596.                 SetDialogControlValue(theDialog, stlPixelsButtonID, 0);
  597.                 SetDialogControlValue(theDialog, ItemHit, 1);
  598.             }
  599.             break;
  600.         case stlHelpButtonID:
  601.             HandleStlHelp();
  602.             SetPort((GrafPtr)thePrDlg);
  603.             RefreshDialog(thePrDlg);
  604.             break;
  605.     }
  606. }
  607.  
  608. Boolean FilePrDlgStl(THPrint hPrint)
  609. {
  610.     TPPrDlg thePrDlg;
  611.     GrafPtr SavePort;
  612.     short itemHit;
  613.     THPrint theDefault;
  614.     Boolean doit = false;
  615.  
  616.     thePrDlg = FilePrStlInit(hPrint);
  617.     if (!thePrDlg) {
  618.         SysBeep(1);
  619.         return(false);
  620.     }
  621.  
  622.     GetPort(&SavePort);
  623.     FrameDialogItem((DialogPtr)thePrDlg, 1);
  624.     while (!(thePrDlg->fDone)) {
  625.         ModalDialog(thePrDlg->pFltrProc, &itemHit);
  626.         CallPItemProc(thePrDlg->pItemProc,(DialogPtr)thePrDlg, itemHit);
  627.     }
  628.  
  629.     SetPort(SavePort);
  630.     if (thePrDlg->fDoIt) {                // save in PREC 1 
  631.         theDefault = (THPrint)ReallyGetResource('PREC', 1);
  632.         if (theDefault != NULL) {
  633.             **theDefault = **hPrint;
  634.             ChangedResource((Handle)theDefault);
  635.             WriteResource((Handle)theDefault);
  636.         }
  637.         doit = true;
  638.     }
  639.  
  640.     DisposeRoutineDescriptor(thePrDlg->pFltrProc);
  641.     DisposeRoutineDescriptor(thePrDlg->pItemProc);
  642.     CloseDialog((DialogPtr)thePrDlg);
  643.     DisposePtr((Ptr)thePrDlg);
  644.  
  645.     return doit;
  646. }
  647.  
  648. pascal TPPrDlg FilePrJobInit(THPrint hPrint)
  649. {
  650.     // we initialize the dialog items with the right settings coming from hPrint 
  651.     DialogPtr aDlg;
  652.     TPPrDlg theDlg;
  653.     SysEnvRec theWorld;
  654.  
  655.     theDlg = (TPPrDlg) NewPtr(sizeof(TPrDlg));
  656.     if (theDlg == NULL) {
  657.         DebugStr("\pCouldn't allocate enough memory for a TPPrDlg. Ummerbay.");
  658.         setPrintErr(iMemFullErr);
  659.         return 0;
  660.     }
  661.  
  662.     theDlg->pFltrProc = NewModalFilterProc(FilterNumPrStlAndJob);
  663.     theDlg->pItemProc = NewPItemProc(HandleJobItems);
  664.     theDlg->fDoIt = false;
  665.     theDlg->fDone = false;
  666.     theDlg->hPrintUsr = hPrint;
  667.  
  668.     aDlg = GetNewDialog(pageSetupDialogID, theDlg, (WindowPtr)-1);
  669.     if (aDlg == NULL) {
  670.         DebugStr("\pCouldn't load the Page Setup dialog. Ummerbay.");
  671.         setPrintErr(resNotFound);
  672.         DisposePtr((Ptr)theDlg);
  673.         return 0;
  674.     }
  675.     DialogPosition(aDlg);
  676.  
  677.     SysEnvirons(1, &theWorld);
  678.  
  679.     if (!theWorld.hasColorQD) {
  680.         HideDItem(aDlg, jobColorButtonID);
  681.         HideDItem(aDlg, jobBlackWhiteButtonID);
  682.         (**hPrint).printX[8] = 1;
  683.     }
  684.  
  685.     if ((**hPrint).printX[6]) {
  686.         SetDialogItemNumber(aDlg, jobFromTextID, (**hPrint).prJob.iFstPage);
  687.         SetDialogItemNumber(aDlg, jobToTextID, (**hPrint).prJob.iLstPage);
  688.         SetDialogControlValue(aDlg, jobAllButtonID, 0);
  689.         SetDialogControlValue(aDlg, jobFromButtonID, 1);
  690.     } else {
  691.         SetDialogControlValue(aDlg, jobAllButtonID, 1);
  692.         SetDialogControlValue(aDlg, jobFromButtonID, 0);
  693.     }
  694.  
  695.     SetDialogControlValue(aDlg, jobColorButtonID, (**hPrint).printX[8] == 0);
  696.     SetDialogControlValue(aDlg, jobBlackWhiteButtonID, (**hPrint).printX[8] == 1);
  697.  
  698.     ShowWindow(aDlg);
  699.     SetPort(aDlg);
  700.     return theDlg;
  701. }
  702.  
  703. pascal void HandleJobItems(TPPrDlg thePrDlg, short ItemHit)
  704. {
  705.     THPrint hPrint = thePrDlg->hPrintUsr;
  706.     DialogPtr theDialog = (DialogPtr)(thePrDlg);
  707.     switch (ItemHit) { 
  708.         case printOkButton:
  709.             if (GetDialogControlValue(theDialog, jobAllButtonID) == 0) {
  710.                 (**hPrint).prJob.iFstPage = GetDialogItemNumber(theDialog, jobFromTextID);
  711.                 (**hPrint).prJob.iLstPage = GetDialogItemNumber(theDialog, jobToTextID);
  712.                 (**hPrint).printX[6] = 1;
  713.             } else {
  714.                 (**hPrint).prJob.iFstPage = iPrPgFst;
  715.                 (**hPrint).prJob.iLstPage = iPrPgMax;
  716.                 (**hPrint).printX[6] = 0;
  717.             }
  718.  
  719.             // We don't handle copies at the moment, but if we did,
  720.             // and we were going to be the one to do multiple copies
  721.             // instead of letting the application do it, this'd be
  722.             // the place where we'd want to stuff the number of copies
  723.             // into a private structure, and put 1 in for the app.
  724.             // If we want the app to handle it, we should fill in the
  725.             // iCopies field of the job record here.
  726.  
  727.             if (GetDialogControlValue(theDialog, jobColorButtonID) == 0) 
  728.                 (**hPrint).printX[8] = 1;
  729.             else 
  730.                 (**hPrint).printX[8] = 0;
  731.  
  732.             thePrDlg->fDone = true;
  733.             thePrDlg->fDoIt = true;
  734.             break;
  735.  
  736.         case printCancelButton:
  737.             thePrDlg->fDone = true;
  738.             thePrDlg->fDoIt = false;
  739.             break;
  740.  
  741.         case jobFromButtonID:
  742.                 SetDialogItemNumber(theDialog, jobFromTextID, (**hPrint).prJob.iFstPage);
  743.                 SetDialogItemNumber(theDialog, jobToTextID, (**hPrint).prJob.iLstPage);
  744.         case jobAllButtonID:
  745.             if (GetDialogControlValue(theDialog, ItemHit) == 0) {
  746.                 SetDialogControlValue(theDialog, ItemHit, 1);
  747.                 SetDialogControlValue(theDialog,
  748.                     (jobAllButtonID+jobFromButtonID) - ItemHit, 0);    // change the other one
  749.             }
  750.             break;
  751.  
  752.         case jobFromTextID:
  753.         case jobToTextID:
  754.             if (GetDialogControlValue(theDialog, jobFromButtonID) == 0) {
  755.                 SetDialogItemNumber(theDialog, jobFromTextID, (**hPrint).prJob.iFstPage);
  756.                 SetDialogItemNumber(theDialog, jobToTextID, (**hPrint).prJob.iLstPage);
  757.                 SetDialogControlValue(theDialog, jobFromButtonID, 1);
  758.                 SetDialogControlValue(theDialog, jobAllButtonID, 0);
  759.             }
  760.             break;
  761.  
  762.         case jobColorButtonID:
  763.         case jobBlackWhiteButtonID:
  764.             if (GetDialogControlValue(theDialog, ItemHit) == 0) {
  765.                 SetDialogControlValue(theDialog, ItemHit, 1);
  766.                 SetDialogControlValue(theDialog,
  767.                     (jobColorButtonID + jobBlackWhiteButtonID) - ItemHit, 0);
  768.             }
  769.             break;
  770.     }
  771. }
  772.  
  773. Boolean FilePrDlgJob(THPrint hPrint)
  774. {
  775.     TPPrDlg thePrDlg;
  776.     GrafPtr SavePort;
  777.     short itemHit;
  778.     Boolean doit = false;
  779.  
  780.     thePrDlg = FilePrJobInit(hPrint);
  781.     if (!thePrDlg) {
  782.         SysBeep(1);
  783.         return(false);
  784.     }
  785.  
  786.     GetPort(&SavePort);
  787.     FrameDialogItem((DialogPtr)thePrDlg,1);
  788.     while (!(thePrDlg->fDone)) {
  789.         ModalDialog(thePrDlg->pFltrProc, &itemHit);
  790.         CallPItemProc(thePrDlg->pItemProc,(DialogPtr)thePrDlg, itemHit);
  791.     }
  792.  
  793.     SetPort(SavePort);
  794.     if (thePrDlg->fDoIt) {
  795.         THPrint theDefault;
  796.  
  797.         theDefault = (THPrint)ReallyGetResource('PREC', 1);
  798.         if (theDefault != NULL) {
  799.             **theDefault = **hPrint;
  800.             ChangedResource((Handle)theDefault);
  801.             WriteResource((Handle)theDefault);
  802.         }
  803.         doit = true;
  804.     }
  805.     DisposeRoutineDescriptor(thePrDlg->pFltrProc);
  806.     DisposeRoutineDescriptor(thePrDlg->pItemProc);
  807.     CloseDialog((DialogPtr)thePrDlg);
  808.     DisposePtr((Ptr)thePrDlg);
  809.  
  810.     return doit;
  811. }
  812.  
  813. // the main 4 entry points follow
  814. pascal Boolean FilePrStlDialog(THPrint hPrint)
  815. {
  816.     return FilePrDlgStl(hPrint);
  817. }
  818.  
  819. pascal Boolean FilePrJobDialog(THPrint hPrint)
  820. {
  821.     return FilePrDlgJob(hPrint);
  822. }
  823.  
  824. pascal void FilePrJobMerge(THPrint hPrintSrc, THPrint hPrintDst)
  825. {
  826.     FilePrValidate(hPrintSrc);
  827.     if (FilePrValidate(hPrintDst)) {    // something changed => it wasn't kosher
  828.         **hPrintDst = **hPrintSrc;        // so just slam in the "good" stuff
  829.     } else {
  830.         (**hPrintDst).prJob = (**hPrintSrc).prJob;    // this is all job specific
  831.         // printX[6] is flag saying whether we're doing a range or all pages
  832.         (**hPrintDst).printX[6] = (**hPrintSrc).printX[6];
  833.     }
  834.     FilePrValidate(hPrintDst);
  835. }
  836.  
  837. pascal Boolean FilePrDlgMain(THPrint hPrint, PDlgInitProcPtr initFunc)
  838. {
  839.     TPPrDlg thePrDlg;
  840.     GrafPtr SavePort;
  841.     short itemHit;
  842.     Boolean doit = false;
  843.  
  844.     thePrDlg = CallPDlgInitProc(initFunc,hPrint);
  845.     if (!thePrDlg) {
  846.         SysBeep(1);
  847.         return(false);
  848.     }
  849.  
  850.     GetPort(&SavePort);
  851.     FrameDialogItem((DialogPtr)thePrDlg,1);
  852.     while (!(thePrDlg->fDone)) {
  853.         ModalDialog(thePrDlg->pFltrProc, &itemHit);
  854.         CallPItemProc(thePrDlg->pItemProc,(DialogPtr)thePrDlg, itemHit);
  855.     }
  856.  
  857.     SetPort(SavePort);
  858.     if (thePrDlg->fDoIt) {
  859.         THPrint theDefault;
  860.  
  861.         theDefault = (THPrint)ReallyGetResource('PREC', 1);
  862.         if (theDefault != NULL) {
  863.             **theDefault = **hPrint;
  864.             ChangedResource((Handle)theDefault);
  865.             WriteResource((Handle)theDefault);
  866.         }
  867.         doit = true;
  868.     }
  869.     DisposeRoutineDescriptor(thePrDlg->pFltrProc);
  870.     DisposeRoutineDescriptor(thePrDlg->pItemProc);
  871.     CloseDialog((DialogPtr)thePrDlg);
  872.     DisposePtr((Ptr)thePrDlg);
  873.  
  874.     return doit;
  875. }
  876.